<?php
	class ThongTinKhuyenMai extends CI_Controller
	{
		function admin_index()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{		
				$this->load->model('thongtinkhuyenmai_model');		
				$data['thongtinkhuyenmai'] = $this->thongtinkhuyenmai_model->LayTatCaKhuyenMai_KhachSan_NhanVien();
								
				$this->load->model('khachsan_model');
				$data['quocgia'] = $this->khachsan_model->LayTatCaQuocGia();
								
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/thongtinkhuyenmai_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function Tim()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = urldecode($this->uri->segment(3));
				
				$this->load->model('thongtinkhuyenmai_model');
				$data['thongtinkhuyenmai'] = $this->thongtinkhuyenmai_model->Tim($key);
								
				$this->load->model('khachsan_model');
				$data['quocgia'] = $this->khachsan_model->LayTatCaQuocGia();				
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/thongtinkhuyenmai_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function LayThongTinKhuyenMai_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('thongtinkhuyenmai_model');
				$data['makm'] = $this->thongtinkhuyenmai_model->LayMaKM_ajax($key);
				$data['tenks'] = $this->thongtinkhuyenmai_model->LayTenKS_ajax($key);
				
				$this->load->view('admin/ajax_danhsachkhuyenmai', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function Xoa()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$makm = $this->uri->segment(3);
				if($makm)
				{
					$data = array('GiaTriKhuyenMai' => 0);
								
					$this->load->model('thongtinkhuyenmai_model');
					$result_1 = $this->thongtinkhuyenmai_model->CapNhat($data, $makm);
					
					$this->load->model('phongtrongtheongay_model');
					$result_2 = $this->phongtrongtheongay_model->CapNhat_GiaSauKhuyenMai($makm);					
								
					$result = $this->thongtinkhuyenmai_model->Xoa($makm);
				
					if($result && $result_1 && $result_2)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	            	    <script>
							alert("Thông tin khuyến mãi đã được xóa!");
							window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/admin_index";
						</script>
	                	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	    	            <script>
							alert("Xóa thông tin khuyến mãi thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/admin_index";
						</script>
	    	            <?php
					}			
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function XoaNhieuDong()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$makms = $this->input->post('checkbox');
				if($makms)
				{
					$this->load->model('thongtinkhuyenmai_model');
					$result = $this->thongtinkhuyenmai_model->XoaNhieuDong($makms);
					
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	            	    <script>
							alert("Thông tin khuyến mãi đã được xóa!");
							window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/admin_index";
						</script>
	                	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	    	            <script>
							alert("Xóa thông tin khuyến mãi thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/admin_index";
						</script>
	    	            <?php
					}			
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function Them()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$timezone = "Asia/Ho_Chi_Minh";
				if(function_exists('date_default_timezone_set')) 
					date_default_timezone_set($timezone);
					
				$giatri = $this->input->post('giatri');
				$ngaybatdau = $this->input->post('ngaybatdau');
				$ngayketthuc = $this->input->post('ngayketthuc');
				
				if($ngaybatdau > $ngayketthuc)
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
            	    <script>
						alert("Lỗi: Ngày kết thúc phải lớn hơn hoặc bằng ngày bắt đầu!");
						window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/admin_index";
					</script>
                	<?php						
					return;						
				}
				
				$noidung = "Giảm " . $giatri . "%";
				
				$ngaydang = date('Y-m-d');
				
				$khuyenmai = array(
									'NoiDung' => $noidung,
									'GiaTriKhuyenMai' => $giatri,
									'NgayBatDau' => $ngaybatdau,
									'NgayKetThuc' => $ngayketthuc,
									'NgayDangTin' => $ngaydang,
									'KhachSan' => $this->input->post('khachsan'),
									'NhanVien' => $this->session->userdata('manv')
								);
								
				$this->load->model('thongtinkhuyenmai_model');
				$makm = $this->thongtinkhuyenmai_model->Them($khuyenmai);
				
				$this->load->model('phongtrongtheongay_model');
				$result = $this->phongtrongtheongay_model->CapNhat_GiaSauKhuyenMai($makm);				
				
				if($makm && $result)
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
            	    <script>
						alert("Thông tin khuyến mãi đã được thêm!");
						window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/admin_index";
					</script>
                	<?php
				}
				else
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
    	            <script>
						alert("Thêm thông tin khuyến mãi thất bại!");
						window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/admin_index";
					</script>
    	            <?php
				}			
			}
			else
				redirect('login/admin_index');			
		}
		
		function MoTrangCapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$makm = $this->uri->segment(3);
				if($makm)
				{
					$this->load->model('thongtinkhuyenmai_model');
					$data['thongtinkhuyenmai'] = $this->thongtinkhuyenmai_model->LayKhuyenMai_KhachSan_NhanVien($makm);
															
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/capnhat_thongtinkhuyenmai_view', $data);
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function CapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$makm = $this->input->post('makm');
									
				$timezone = "Asia/Ho_Chi_Minh";
				if(function_exists('date_default_timezone_set')) 
					date_default_timezone_set($timezone);
					
				$giatri = $this->input->post('giatri');	
				
				$noidung = "Giảm " . $giatri . "%";					
				
				$ngaydang = date('Y-m-d');
				
				$data = array(
								'NoiDung' => $noidung,
								'GiaTriKhuyenMai' => $giatri,
								'NgayDangTin' => $ngaydang,
								'NhanVien' => $this->session->userdata('manv')
							);
								
				$this->load->model('thongtinkhuyenmai_model');
				$result_1 = $this->thongtinkhuyenmai_model->CapNhat($data, $makm);
				
				$this->load->model('phongtrongtheongay_model');
				$result_2 = $this->phongtrongtheongay_model->CapNhat_GiaSauKhuyenMai($makm);
				
				if($result_1 < 0 || $result_2 < 0)
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
                    <script>
						alert('Thay đổi thông tin thất bại!');
						window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/MoTrangCapNhat/<?php echo $makm; ?>";						
					</script>
                    <?php
					return;
				}
				
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				?>
                <script>
					alert('Thông tin đã được thay đổi!');
					window.location = "<?php echo base_url(); ?>index.php/thongtinkhuyenmai/MoTrangCapNhat/<?php echo $makm; ?>";						
				</script>
                <?php				
			}
			else
				redirect('login/admin_index');			
		}
	}
?>